<?php
class Category extends Tut_Model {
    public $id;
    public $name;
    public $parent_id;
    public $is_actived;
    public $description;
    public $date;
    public $last_update;

    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    public function add(array $aCategory) {
        if (empty($aCategory)) {
            return false;
        }

        $aCategory = $this->_prepareArr($aCategory);
        if ((bool) $this->db->insert('product_category', $aCategory)) {
            return $aCategory;
        }

        return false;
    }

    public function edit(array $aProduct) {
        if (empty($aProduct)) {
            return false;
        }

        //Get id product because after prepareArr, it's going to unset.
        $iId = $aProduct['id'];
        $aProduct = $this->_prepareArr($aProduct, true);
        if ((bool) $this->db->update('product', $aProduct, array('id' => $iId))) {
            return $aProduct;
        }

        return false;
    }

    public function save($iId, $iUserId) {
        if (empty($iId) || empty($iUserId)) {
            return false;
        }

        $aSavedProduct = array(
            'user_id' => $iUserId,
            'product_id' => $iId,
            'date' => time()
        );

        if ((bool) $this->db->insert('saved_product', $aSavedProduct)) {
            return $iId;
        }

        return false;
    }

    public function delete($aIds) {
        if (empty($aIds)) {
            return false;
        }

//        $query = "DELETE FROM tut_product WHERE id IN( " . implode(',', array_values($aIds))  . ")";
        $sql = "UPDATE tut_product SET is_deleted=1 WHERE id IN( " . implode(',', array_values($aIds)) . ")";
        $query = $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return true;
        }

        return false;
    }

    public function getById($iId = 0, $bIsCheckActive = true) {
        if (empty($iId)) {
            return false;
        }

        $this->db->select('p.*, pc.name as product_category, le.name as level, l.name as language')
                ->from('product as p')
                ->join('product_category as pc', 'p.product_category_id = pc.id')
                ->join('language as l', 'p.language_id = l.id')
                ->join('level le', 'p.level_id = le.id')
                ->where('p.id', $iId);

        if ($bIsCheckActive) {
            $this->db->where('p.is_actived', ACTIVE);
        }

        $product = $this->db->get();

        if ($product->num_rows() > 0) {
            $product = $product->row();
            $product->date = date('Y-m-d', $product->date);

            return $product;
        }

        return false;
    }

    private function _prepareArr(array $aArr = null, $isEdit = false) {
        if (empty($aArr)) {
            return false;
        }

        //Unset id to empty.
        if (!$isEdit) {
            unset($aArr['id']);
        }
        
        $aArr['is_actived'] = $aArr['actived'];
        unset($aArr['actived']);
        $aArr['date'] = time();
        $aArr['last_update'] = time();
        return $aArr;
    }

    public function gets($offset = 0, $numRow = 10) {
        $oProduct = $this->db->select('SQL_CALC_FOUND_ROWS p.id, p.name, p.summary, p.description,
            pc.name as category, p.total_view, p.total_like, p.date, p.image, sp.user_id as user_saved', false)
                ->from('product p')
                ->join('product_category pc', 'p.product_category_id = pc.id', 'left')
                ->join('language l', 'p.language_id = l.id', 'left')
                ->join('saved_product sp', 'p.id = sp.product_id', 'left')
                ->join('level le', 'p.level_id = le.id', 'left')
                ->where(array('p.is_actived' => 1, 'p.is_deleted' => 0))
                ->limit($numRow, $offset)
                ->order_by('order', 'asc')
                ->get();


        $aProduct = array();
        $this->load->library('encrypt');
        if ($oProduct->num_rows() > 0) {
            foreach ($oProduct->result() as $product) {
                $product->date = date('Y-m-d', $product->date);
                //$product->id = $this->encrypt->encode($product->id, $this->config->item('encode_id'));
                $aProduct[] = $product;
            }
        }

        $this->_itotal_rows = $this->db->query('SELECT FOUND_ROWS() AS count', false)->row()->count;

        return $aProduct;
    }

    public function getListAdmin($offset, $aSearch = null) {
        $this->db->select('SQL_CALC_FOUND_ROWS p.id, p.name, pc.name as category, p.date, p.is_actived as actived,
            p.is_featured as featured, p.order, l.name as language,
            le.name as level, p.date', false)
                ->from('product p')
                ->join('product_category pc', 'p.product_category_id = pc.id', 'left')
                ->join('language l', 'p.language_id = l.id', 'left')
                ->join('level le', 'p.level_id = le.id', 'left')
                ->where('is_deleted', 0);

        $aSearch = $this->prepareSearch($aSearch);

        foreach ($aSearch as $key => $val) {
            if (trim($val) == '') {
                continue;
            }

            switch ($key) {
                case 'category':
                case 'p.name':
                    $this->db->like($key, $val);
                    break;
                default :
                    $this->db->where($key, $val);
                    break;
            }
        }

        $this->db->limit($this->config->item('per_page_admin'), ($offset - 1) * $this->config->item('per_page_admin'));

//        var_dump($this->_aSort);
        foreach ($this->_aSort as $key => $value) {
            $this->db->order_by($this->_aRealSort[$key], $value);
        }

        $oProduct = $this->db->get();
        $aProduct = array();
        
        if ($oProduct->num_rows() > 0) {
            foreach ($oProduct->result() as $product) {
                $product->date = date('Y-m-d', $product->date);
                $aProduct[] = $product;
            }
        }

        $this->_itotal_rows = $this->db->query('SELECT FOUND_ROWS() AS count', false)->row()->count;

        return $aProduct;
    }
    
    private function _load() {
    }
}

?>